package com.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.mapper.SubMapper;
import com.gxa.model.ResultData;
import com.gxa.pojo.Grade;
import com.gxa.pojo.Subjects;
import com.gxa.service.SubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周区区
 * @version 1.0
 * @date 2021/8/4 10:41
 */
@Service
public class SubServiceImpl implements SubService {
    @Autowired
    private SubMapper subMapper;
    @Override
    public ResultData findAllSub() {
        //查询构造器
        QueryWrapper qw = new QueryWrapper();
        //key：数据库列名  value：数据库的值
        Map<String, Object> map = new HashMap<>();
        //把查询条件放到查询构造器中
        qw.allEq(map,false);
        //调用MP的查询方法
        List<Subjects> allSub = subMapper.selectList(qw);
        long count = subMapper.selectCount(qw);
        if(allSub.size()>0){
            return ResultData.ok().data(allSub).count(count).message("查找成功");
        }
        return ResultData.error().message("查找失败");
    }

    @Override
    public ResultData delSub(Integer subId) {
        int i = subMapper.deleteById(subId);
        if (i>0){
            return ResultData.ok().message("删除成功");
        }else{
            return ResultData.error().message("删除失败");
        }
    }

    @Override
    public ResultData updSub(Subjects subjects) {
        int i = subMapper.updateById(subjects);
        if (i>0){
            return ResultData.ok().message("修改成功");
        }else{
            return ResultData.error().message("修改失败");
        }
    }

    @Override
    public ResultData addSub(Subjects subjects) {
        int i = subMapper.insert(subjects);
        if (i>0){
            return ResultData.ok().message("添加成功");
        }else{
            return ResultData.error().message("添加失败");
        }
    }
}
